package com.beiju.coupon.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beiju.coupon.entity.Business;
import com.beiju.coupon.mapper.BusinessMapper;
import com.beiju.coupon.service.IBusinessService;
import com.beiju.coupon.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * 商家信息表服务实现层
 *
 * @author beiju
 * @date 2021-01-22
 */
@Slf4j
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements IBusinessService
{
	@Resource
	private BusinessMapper businessMapper;
    /**
     * 查询商家信息表列表
     */
	@Override
    public Result list(JSONObject params){
		log.info("列表查询开始，参数：{}",params);
		QueryWrapper<Business> queryWrapper = new QueryWrapper<>();
		//TODO 填写查询条件
		List<Business> list = businessMapper.selectList(queryWrapper);
		log.info("列表查询结束");
    	return Result.success("列表查询成功",list);
    }

	/**
	 * 查询所有的商家信息详细内容
	 *
	 * @param params
	 */
	@Override
	public Result getAll(JSONObject params) {
		log.info("查询所有信息开始，参数：{}",params);
		QueryWrapper<Business> queryWrapper = new QueryWrapper<>();
		//TODO 填写查询条件

		List<Business> list = businessMapper.queryAll();
		log.info("查询所有信息结束");
		return  Result.success("查询所有信息成功",list);
	}


	/**
     * 获取商家信息表详细信息
     */
	@Override
    public Result getInfo(JSONObject params){
		log.info("指定查询开始，参数：{}",params);
		// TODO 根据实际情况填写JSON对呀的key值
		Integer id = params.getInt("id");
		Business business = businessMapper.selectById(id);
		log.info("指定查询结束");
		return Result.success("查询成功",business);
	}


    /**
     * 新增商家信息表
     */
	@Override
    public Result add(JSONObject params){
		log.info("新增开始，参数：{}",params);
		Business business = params.toBean(Business.class);
		// TODO 对参数进行校验
		businessMapper.insert(business);
		log.info("新增结束");
		return Result.success("新增成功");
    }


    /**
     * 修改商家信息表
     */
	@Override
    public Result edit(JSONObject params){
		log.info("修改开始，参数：{}",params);
		Business business = params.toBean(Business.class);
		// TODO 对参数进行校验
		int i = businessMapper.updateById(business);
		log.info("修改结束");
		return i > 0 ? Result.success("修改成功"):Result.error("修改失败");
    }


    /**
     * 删除商家信息表
     */
	@Override
     public Result remove(JSONObject params){
		log.info("删除开始，参数：{}",params);
		// TODO 根据实际情况填写JSON对呀的key值
		List<Integer> ids = JSONUtil.toList(params.getJSONArray("ids"), Integer.class);
		if (CollUtil.isEmpty(ids)){
			return Result.error("参数[ids]不能为空");
		}
		int i = businessMapper.deleteBatchIds(ids);
		log.info("删除结束");
		return i > 0 ? Result.success("删除成功"):Result.error("删除失败");
     }

}
